package com.glc.hms.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.glc.hms.common.exception.ServiceException;
import com.glc.hms.common.page.PageFactory;
import com.glc.hms.system.dao.SysRoleMapper;
import com.glc.hms.system.dao.SysUserRoleMapper;
import com.glc.hms.system.entity.SysRole;
import com.glc.hms.system.entity.SysUserRole;
import com.glc.hms.system.model.param.UpdateRoleMenuParam;
import com.glc.hms.system.service.ISysMenuService;
import com.glc.hms.system.service.ISysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 角色 服务实现类
 * </p>
 *
 * @author Santa
 * @since 2019-11-20
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements ISysRoleService {

    @Autowired
    private SysRoleMapper sysRoleMapper;
    @Autowired
    private ISysMenuService menuService;
    @Autowired
    private SysUserRoleMapper userRoleMapper;


    /**
     * 查询角色列表
     * @return
     */
    @Override
    public Page<Map<String, Object>> queryRoleByPage(){
        Page page = PageFactory.defaultPage();
        Page<Map<String, Object>> mapPage = sysRoleMapper.queryRoleByPage(page);
        return mapPage;
    }


    /**
     * 删除角色权限
     * @param param
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateRoleMenu(UpdateRoleMenuParam param){

        if(param.getMenuId()!=null){
            //-1 删除一级菜单  0 删除二级菜单  其他删除菜单动作
            menuService.deleRoleMenu(param.getRoleId(),param.getMenuId(),param.getActionId());
        }else {
            menuService.addRoleMenu(param.getRoleId(),param.getMenuAndAction());
        }
    }


    /**
     * 修改用户角色
     * @param userId
     * @param newRoleId
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateUserRole(Long userId , Long newRoleId){
        QueryWrapper<SysUserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        SysUserRole sysUserRole = userRoleMapper.selectOne(queryWrapper);
        sysUserRole.setRoleId(newRoleId);
        userRoleMapper.updateById(sysUserRole);
    }

    /**
     * 添加用户角色
     * @param userId
     * @param newRoleId
     */
    @Override
    public void addUserRole(Long userId , Long newRoleId){
        SysUserRole sysUserRole = new SysUserRole();
        sysUserRole.setRoleId(newRoleId);
        sysUserRole.setUserId(userId);
        userRoleMapper.insert(sysUserRole);
    }


    /**
     * 删除角色
     * @param roleId
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delUserRole(Long roleId){
        QueryWrapper<SysUserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id",roleId);
        List<SysUserRole> sysUserRoles = userRoleMapper.selectList(queryWrapper);
        if(!CollectionUtils.isEmpty(sysUserRoles)){
            throw new ServiceException(500,"角色下有用户,无法删除");
        }else {
            this.removeById(roleId);
        }
    }
	
}
